angular.module('Controller')
.directive('contractPaymentForm',[()->
    return {
        restrict:'E'
        replace:true
        scope:{
            contractData:'=contractData'
            billId:'=billId'
        }
        templateUrl:'templates/common-directive/contract-payment-form/contract-payment-form.html'
        controller:'contractPaymentFormCtrl'
    }
])

.controller 'contractPaymentFormCtrl',(urls,$scope,wfAlert,userData,commonUtil,commonConstant,$http,$localStorage)->
    $scope.offPayTypeList=commonConstant.offPayTypeList()
    loadBill=(id)->
        return if id==undefined
        $http.get("/bill/#{id}")
        .success((resp)->
            if resp.code==0
                $scope.bill=resp.message
                $scope.bill.payType=$scope.offPayTypeList[0].value
        )
    loadBill($scope.billId)
    paying=false
    $scope.doPayment=()->
        if $scope.contractData and $scope.bill and $scope.contractData.status==2 and paying==false and $scope.bill.status==1
            paying=true
            reqData = {
                payMoney: $scope.bill.money
                payType: $scope.bill.payType
                moneyDesc: $scope.bill.moneyDesc
            }
            $http.put("/bill/#{$scope.bill.id}/pay", reqData)
            .success((resp)->
                paying=false
                if resp.code == 0
                    wfAlert.toastInfo('确认收款成功')
                    $scope.bill.status=2
                    $scope.contractData.status = 4
                else
                    wfAlert.toastError(resp.message)
            )
            .error((err)->
                paying=false
                console.log('确认收款失败',err)
                wfAlert.toastError('网络异常')
            )

